Generated code - Using the typed list classes, Adapter

Preface

LLBLGen Pro supports read-only lists based on a database view (Typed View) or a selection of entity fields from one or more entities with a relation (not m:n) (Typed List). Although both elements are different, they both will be generated as a typed DataTable, which is a variant of the typed DataSet concept included in Visual Studio.NET. A typed DataTable is a class which derives from the .NET DataTable and defines properties and a row class to access the individual fields in a typed fashion.

In this section the typed list classes are briefly discussed and their usage is illustrated using examples. It's recommended you read the Using the typed view classes, SelfServicing section as well, as it discusses constructs which are shared with the Typed List.

Instantiating and using a Typed List

Using a Typed List is similar to using a Typed View, both will be generated as typed DataTables. There is a difference however in how you formulate filters and sort clauses for a typed list. The reason for this is that a typed list is constructed using existing entity fields, while a typed view uses its own field definitions. If you want to construct a filter for a typed list, you have to specify field indexes of fields in the entities which are the base of the typed list; you can filter on fields which are not included in the typed list itself as in the column set of the typed list. A Typed List contains code to help you construct the RelationPredicateBucket objects for filtering. Use the GetRelationInfo() method of the Typed List object to get an initial bucket with essential information for fetching the data of the Typed List. Because RelationPredicateBucket objects contain an already existing PredicateExpression object you can directly start adding filter predicates to the bucket.

A Typed List is filled similar to the Typed View objects, with one difference: you should use DataAccessAdapter.FetchTypedList() instead of DataAccessAdapter.FetchTypedView().

As an example of this, we construct a typed list from the entities Customer and Order and include the following fields in the resultset: Order.OrderID, Order.OrderDate, Order.ShippedDate, Customer.CustomerID, Customer.CompanyName. (Do this by checking these fields in the field list in the typed list editor). You can now filter on any field in Order or Customer or both. Also, you can sort on any field in Order or Customer or both. Let's filter this typed list on all orders from customers from 'Brazil', and sort the list on the field Order.Freight, ascending. The typed list is called OrderCustomer.

// [C#]
OrderCustomerTypedList orderCustomer = new OrderCustomerTypedList();
DataAccessAdapter adapter = new DataAccessAdapter();
IRelationPredicateBucket bucket = orderCustomer.GetRelationInfo();
bucket.PredicateExpression.Add(CustomerFields.Country == "Brazil");
ISortExpression sorter = new SortExpression(OrderFields.OrderId | SortOperator.Ascending);
// Set allowDuplicates to true, because we sort on a field that is not in our resultset and we use SqlServer.
adapter.FetchTypedList(orderCustomer.GetFieldsInfo(), orderCustomer, bucket, 0, sorter, false);
' [VB.NET]
Dim orderCustomer As New OrderCustomerTypedList()
Dim adapter As New DataAccessAdapter()
Dim bucket As IRelationPredicateBucket = orderCustomer.GetRelationInfo()
bucket.PredicateExpression.Add(New FieldCompareValuePredicate(CustomerFields.Country, Nothing, ComparisonOperator.Equal, "Brazil"))
Dim sorter As ISortExpression = New SortExpression(New SortClause(OrderFields.OrderId, Nothing, SortOperator.Ascending))
' Set allowDuplicates to true, because we sort on a field that is not in our resultset and we use SqlServer.
adapter.FetchTypedList(orderCustomer.GetFieldsInfo(), orderCustomer, bucket, 0, sorter, False)

The Typed List object is now filled with the rows for the 5 columns we've specified in the Typed List editor, sorted on Order.Freight ascending and filtered on Customer.Country equals "Brazil".

note Note:
If you're using one of the FetchTypedList overloads of DataAccessAdapter which accepts IEntityFields2 and an IRelationPredicateBucket, you have to pass the object you get from typedlist.GetFieldsInfo() as the fieldCollectionToFetch value and the object you get from typedlist.GetRelationInfo() as the filterBucket parameter. Otherwise relations of the typed list aren't detected. If you don't want to filter the typedlist, use the overloads of FetchTypedList which accept a typedListToFill parameter. See the reference manual for details.

note Note:
TypedLists still offer the functionality of Weak Relations through the property ObeyWeakRelations. (See for a description of weak relations this section in Filtering and Sorting). It's however recommended to use the JoinHint specifications for the relations in the TypedList editor in the LLBLGen Pro Designer.


LLBLGen Pro v2.6 documentation. ©2002-2008 Solutions Design